package com.flashidea.zddziot.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;

import com.flashidea.zddziot.R;
import com.flashidea.zddziot.utlis.CMDUtils;
import com.flashidea.zddziot.view.OnAngleChangeListener;
import com.flashidea.zddziot.view.RoundVolumeView;

/**
 * 音效调节界面
 * 
 * @author jinhui
 */
public class SoundEffectActivity extends BluetoothChatActivity
		implements
			OnAngleChangeListener{

	private static final String TAG = "SoundEffectActivity";
	private static final String BAS_ANGLE = "low_angle";
	private static final String TRE_ANGLE = "high_angle";
	private static final String BAL_ANGLE = "lr_angle";
	private static final String FAD_ANGLE = "fb_angle";
	private static final int CHANGE_ANGLE = 30;
	/**
	 * 低音
	 */
	private RoundVolumeView rvvBAS;
	/**
	 * 高音
	 */
	private RoundVolumeView rvvTRE;
	/**
	 * 左右
	 */
	private RoundVolumeView rvvBAL;
	/**
	 * 前后
	 */
	private RoundVolumeView rvvFAD;
	private SharedPreferences sp;
	private int currBASAngle;
	private int currTREAngle;
	private int currBALAngle;
	private int currFADAngle;
	private TextView tv_bas;
	private TextView tv_bal;
	private TextView tv_tre;
	private TextView tv_fad;

	private String NULL_VALUE = "- - -";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sound_effect);
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);

	}

	@Override
	protected void initView() {
		findViewById(R.id.tv_back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		rvvBAS = (RoundVolumeView) findViewById(R.id.rvv_BAS);
		rvvTRE = (RoundVolumeView) findViewById(R.id.rvv_TRE);
		rvvBAL = (RoundVolumeView) findViewById(R.id.rvv_BAL);
		rvvFAD = (RoundVolumeView) findViewById(R.id.rvv_FAD);
		rvvBAS.setOnAngleChangeListener(this);
		rvvTRE.setOnAngleChangeListener(this);
		rvvBAL.setOnAngleChangeListener(this);
		rvvFAD.setOnAngleChangeListener(this);
		
		tv_bas = (TextView) findViewById(R.id.tv_bas_value);
		tv_bal = (TextView) findViewById(R.id.tv_bal_value);
		tv_tre = (TextView) findViewById(R.id.tv_tre_value);
		tv_fad = (TextView) findViewById(R.id.tv_fad_value);
		
		//发送同步码
		sendMessage(CMDUtils.SYNC);
	}
	

	@Override
	protected synchronized void onResume() {
		super.onResume();
		rvvBAS.setAngle(sp.getInt(BAS_ANGLE, -90),false);
		rvvTRE.setAngle(sp.getInt(TRE_ANGLE, -90),false);
		rvvBAL.setAngle(sp.getInt(BAL_ANGLE, -90),false);
		rvvFAD.setAngle(sp.getInt(FAD_ANGLE, -90),false);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Editor edit = sp.edit();
		edit.putInt(BAS_ANGLE, currBASAngle);
		edit.putInt(TRE_ANGLE, currTREAngle);
		edit.putInt(BAL_ANGLE, currBALAngle);
		edit.putInt(FAD_ANGLE, currFADAngle);
		edit.commit();
	}
	private int bas_change = 0;
	private int tre_change = 0;
	private int bal_change = 0;
	private int fad_change = 0;
	@Override
	public void onAngleChange(Object view, int viewId, int angle, int degree) {
		String messsage = "";
		if (view == rvvBAS) {
			if (D)
				Log.i(TAG, "低音," + angle + "," + degree);
			if (Math.abs(angle - currBASAngle) >= CHANGE_ANGLE) { // 6个角度发送一次
				if (degree > 0) {
					messsage = CMDUtils.BAS_CLOCKWISE;
					++bas_change;
				} else {
					messsage = CMDUtils.BAS_COUNTERCLOCKWISE;
					--bas_change;
				}
				currBASAngle = angle;
				// 每给两个角度发一次指令
				sendMessage(messsage);
				//更新显示
				updateChangeValue(tv_bas,bas_change);
			}
		} else if (view == rvvTRE) {
			Log.i(TAG, "高音," + angle + "," + degree);
			if (Math.abs(angle - currTREAngle) >= CHANGE_ANGLE) { // 6个角度发送一次
				if (degree > 0) {
					messsage = CMDUtils.TRE_CLOCKWISE;
					++tre_change;
				} else {
					messsage = CMDUtils.TRE_COUNTERCLOCKWISE;
					--tre_change;
				}
				currTREAngle = angle;
				// 每给两个角度发一次指令
				sendMessage(messsage);
				//更新显示
				updateChangeValue(tv_tre,tre_change);
			}
		} else if (view == rvvBAL) {
			Log.i(TAG, "左右," + angle + "," + degree);
			if (Math.abs(angle - currBALAngle) >= CHANGE_ANGLE) { // 6个角度发送一次
				if (degree > 0) {
					messsage = CMDUtils.BAL_CLOCKWISE;
					++bal_change;
				} else {
					messsage = CMDUtils.BAL_COUNTERCLOCKWISE;
					--bal_change;
				}
				currBALAngle = angle;
				// 每给两个角度发一次指令
				sendMessage(messsage);
				//更新显示
				updateChangeValue(tv_bal,bal_change);
			}
		} else {
			Log.i(TAG, "前后," + angle + "," + degree);
			if (Math.abs(angle - currFADAngle) >= CHANGE_ANGLE) { // 6个角度发送一次
				if (degree > 0) {
					messsage = CMDUtils.FAD_CLOCKWISE;
					++fad_change;
				} else {
					messsage = CMDUtils.FAD_COUNTERCLOCKWISE;
					--fad_change;
				}
				currFADAngle = angle;
				// 每给两个角度发一次指令
				sendMessage(messsage);
				//更新显示
				updateChangeValue(tv_fad,fad_change);
			}
		}
	}
	/**
	 * 更新显示增长的数值
	 * @param textView
	 * @param currValue
	 */
	private void updateChangeValue(TextView textView, int currValue) {
		//改为比较浅的颜色
//		textView.setTextColor(getResources().getColor(R.color.transparent_white4));
//		int abs = Math.abs(currValue);
//		if(currValue < 0){ // (-1)
//			textView.setText("[ - " + abs + " ]");
//		}else{ // >=0 (+20)
//			textView.setText("[ + " + abs + " ]");
//		}
		textView.setText(NULL_VALUE);
	}

	/**
	 * 当发送指令后
	 */
	@Override
	protected void onAfterWriteMessage(String writeMessage) {
	}

	/**
	 * 当连接的设备发送改变时
	 */
	@Override
	protected void onConnectedDeviceNameChange(String mConnectedDeviceName2) {
	}

	/**
	 * 当接收到来自已连接的蓝牙设备的信息
	 */
	@Override
	protected void onReadMessage(String readMessage) {
		if (readMessage.startsWith(CMDUtils.READ_BAS_HEAD)) {
			tv_bas.setText(String.valueOf(CMDUtils.getBASValue(readMessage)));
			tv_bas.setTextColor(getResources().getColor(R.color.white));
			bas_change = 0;
		}else if (readMessage.startsWith(CMDUtils.READ_TRE_HEAD)) {
			tv_tre.setText(String.valueOf(CMDUtils.getTREValue(readMessage)));
			tv_tre.setTextColor(getResources().getColor(R.color.white));
			tre_change = 0;
		}else if (readMessage.startsWith(CMDUtils.READ_BAL_HEAD)) {
			tv_bal.setText(CMDUtils.getBALValue(readMessage));
			tv_bal.setTextColor(getResources().getColor(R.color.white));
			bal_change = 0;
		}else if (readMessage.startsWith(CMDUtils.READ_FAD_HEAD)) {
			tv_fad.setText(CMDUtils.getFADValue(readMessage));
			tv_fad.setTextColor(getResources().getColor(R.color.white));
			fad_change = 0;
		}
	}

}
